package org.brisling.duties.queryService;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpSession;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.duties.domain.DutiesComplain;
import org.brisling.duties.domain.DutiesDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/dutiesComplain/query")
public class QueryDutieComplainService extends AbstractBaseQueryService<DutiesComplain>{
	
	
	
	
	
	public QueryDutieComplainService(){
		this.moduleName = ConstSysParam.MODULE_DUTIES_COMPLAIN;
		this.subSystem = ConstSysParam.SUBSYSTEM_DUTIES;
		this.tableName = this.moduleName;
		this.clazz = DutiesComplain.class;
		
		
		
		
		orderMap.put("createtime","desc");
			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<DutiesComplain> getListDomain(
			@RequestBody(required=false) String body,HttpSession session){
		
		String userid = getUserid(session);
		Map<String,Object> conditionMap =this.getBodyParam(body); 
		if(!this.isSuperDept(userid,"query_all", "query_duties_all"))		
			conditionMap=this.addDeptConditionToMap(userid,conditionMap, "doct_depart_code");
		
		return super.getListDomainByConditionAndOrder(null,conditionMap, this.getTypeMap(DutiesDomain.class), orderMap,true);
		
		
	}
	
	
	
	/**
	 * 获取实体列表对象
	 * @param page
	 * @param rows
	 * @param deptid
	 * @param sort
	 * @return
	 */
	
	@RequestMapping("/getList.action")
	public List<DutiesComplain> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
}
